﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TS.Commons;
using TS.Entities;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using System.Globalization;

namespace TS.DataAccess
{
    /// <summary>
    /// Author: VuongPT
    /// Creation Date: 12/02/2013
    /// </summary>
    public class GatewayDataAccess
    {
        /// <summary>
        /// Parse object to Entity
        /// </summary>
        /// <param name="dataRecord"></param>
        /// <returns></returns>
        public static Gateway ParseToEntity(IDataRecord dataRecord)
        {
            return new Gateway
            {
                GatewayId = dataRecord["GatewayId"].ToStringOrDefault().ToNumeric(),
                Name = dataRecord["Name"].ToStringOrDefault(),
                Details = dataRecord["Detail"].ToStringOrDefault(),
                Description = dataRecord["Description"].ToStringOrDefault()
            };
        }


        /// <summary>
        /// Select All Command
        /// </summary>
        /// <returns></returns>
        public Collection<Gateway> GetAllGateway()
        {
            Collection<SqlParameter> collection = new Collection<SqlParameter>();

            int totalRecord = 0;
            using (IDataReader reader = SqlHelper.ExecuteReader(UIGateway.SelectAllGatewayQuery, collection))
            {
                var result = new Collection<Gateway>();
                while (reader.Read())
                {
                    Gateway gtw = ParseToEntity(reader);
                    result.Add(gtw);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        totalRecord = reader["TotalRowCount"].ToNumeric();
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// Read Gateway Pagging
        /// </summary>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="strFilter"></param>
        /// <param name="strOrderBy"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public Collection<Gateway> ReadGateWayPaged(int pageSize, int pageIndex, string strFilter, string strOrderBy, out int totalRecord)
        {
            var parameters = new Collection<SqlParameter>
                                                      {
                                                          new SqlParameter("@PageSize",pageSize),
                                                          new SqlParameter("@PageIndex",pageIndex),
                                                          new SqlParameter("@StringFilter",strFilter),
                                                          new SqlParameter("@OrderBy",strOrderBy)
                                                      };

            totalRecord = 0;

            //get result
            using (IDataReader reader = SqlHelper.ExecuteReader(UIGateway.GatewaySelectPaged, parameters))
            {
                var result = new Collection<Gateway>();
                while (reader.Read())
                {
                    Gateway gateway = ParseToEntity(reader);
                    gateway.ShortDescription = Utility.WriteString(gateway.Description, 20);
                    gateway.ShortDetails = Utility.WriteString(gateway.Details, 20);
                    result.Add(gateway);
                }
                if (reader.NextResult())
                {
                    if (reader.Read())
                    {
                        totalRecord = reader["TotalRowCount"].ToNumeric();
                    }
                }
                return result;
            }
        }

        /// <summary>
        /// Insert Command
        /// </summary>
        /// <returns></returns>
        public int InsertGateway(Gateway gateway)
        {
            int result = 0;
            if (gateway != null)
            {
                var parameters = new Collection<SqlParameter>
                                                          {
                                                              new SqlParameter("@Name",gateway.Name),
                                                              new SqlParameter("@Details",gateway.Details),
                                                              new SqlParameter("@Description",gateway.Description)
                                                          };

                using (IDataReader reader = SqlHelper.ExecuteReader(UIGateway.InsertGatewayQuery, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["GatewayId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="gateway"></param>
        /// <returns></returns>
        public int UpdateGateway(Gateway gateway)
        {
            int result = 0;
            if (gateway != null)
            {
                var parameters = new Collection<SqlParameter>
                                                          {
                                                              new SqlParameter("@GatewayId",gateway.GatewayId),
                                                              new SqlParameter("@Name",gateway.Name),
                                                              new SqlParameter("@Details",gateway.Details),
                                                              new SqlParameter("@Description",gateway.Description)
                                                          };

                using (IDataReader reader = SqlHelper.ExecuteReader(UIGateway.UpdateGatewayQuery, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["GatewayId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            }

            return result;
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="gatewayId"></param>
        /// <returns></returns>
        public Gateway GetByGatewayId(int gatewayId)
        {
            var collection = new Collection<SqlParameter>
                                                      {
                                                          new SqlParameter
                                                              (
                                                              "@GatewayId",
                                                              gatewayId
                                                              )
                                                      };

            using (IDataReader iDataReader = SqlHelper.ExecuteReader(UIGateway.SelectByIdQuery, collection))
            {
                if (iDataReader.Read())
                {
                    return ParseToEntity(iDataReader);
                }
            }
            return null;
        }

        /// <summary>
        /// Delete Gateway
        /// </summary>
        /// <param name="gateway"></param>
        /// <returns></returns>
        public int DeleteGateway(Gateway gateway)
        {
            int result = 0;
            if (gateway != null)
            {
                var parameters = new Collection<SqlParameter>
                                                          {
                                                              new SqlParameter("@GatewayId",gateway.GatewayId)
                                                          };

                using (IDataReader reader = SqlHelper.ExecuteReader(UIGateway.DeleteByIdQuery, parameters))
                {
                    if (reader != null)
                    {
                        while (reader.Read())
                        {
                            result = Convert.ToInt32(reader["GatewayId"], CultureInfo.CurrentCulture);
                        }
                        return result;
                    }
                }
            }

            return result;
        }
    }
}
